<%--
  Created by IntelliJ IDEA.
  User: Fmc
  Date: 2022/6/25
  Time: 15:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>角色管理</title>
    <link rel="stylesheet" href="../../static/layui-main/dist/css/layui.css">
    <script src="../../static/layui-main/src/layui.js"></script>
</head>
<body>

<!-- 搜索框 -->
<form class="layui-form" action="" id="searchForm">
    <div class="layui-inline">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-inline">
            <input type="text" id="roleName" name="roleName"  placeholder="角色名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" id="createBy" name="createBy"  placeholder="创建人" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">修改人</label>
        <div class="layui-input-inline">
            <input type="text" id="updateBy" name="updateBy"  placeholder="修改人" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="formDemo" id="reserch">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="search_reset">重置</button>
        </div>
    </div>
</form>
<!-- 头部工具栏 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn  layui-icon layui-icon-add-circle" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-danger layui-icon layui-icon-delete" lay-event="delete">删除</button>
    </div>
</script>

<!-- 行内工具栏 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-dange layui-btn-xs layui-icon layui-icon-edit" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-icon layui-icon-delete" lay-event="del">删除</a>
</script>

<!-- 添加时，弹出的表单 -->
<div style="width: 650px; position: relative;">
    <form class="layui-form" action="" id="addRoleFrom" lay-filter="addRoleFromFilter" style="display: none">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-inline">
                <input type="text" name="roleName" required lay-verify="required" placeholder="请输入角色名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">关键字</label>
            <div class="layui-input-inline">
                <input type="text" name="roleKey" required lay-verify="required" placeholder="请输入角色关键字" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">显示顺序</label>
            <div class="layui-input-inline">
                <input type="text" name="roleSort" required lay-verify="required" placeholder="请输入显示顺序" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchAdd" lay-text="可用|不可用">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addRoleFrom">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary" id="resetAdd">重置</button>
            </div>
        </div>
    </form>
</div>

<!-- 修改时，弹出的表单 -->
<form class="layui-form" action="" lay-filter="updateRoleFilter" id="updateRoleForm" style="display: none">

    <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-inline">
            <input type="text" name="roleId" disabled required lay-verify="required" readonly class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-inline">
            <input type="text" name="roleName" required lay-verify="required"  class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">关键字</label>
        <div class="layui-input-inline">
            <input type="text" name="roleKey" required lay-verify="required"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示顺序</label>
        <div class="layui-input-inline">
            <input type="text" name="roleSort" required lay-verify="required"  class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="checkbox"  name="status" lay-skin="switch" lay-filter="switchUpdate" lay-text="可用|不可用">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateRole">保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!-- table表格 -->
<%--lay-filter相当于dom元素的id，layui专用的选择器--%>
<table id="demo" lay-filter="test"></table>

</body>
<script>
    layui.use(['table','form','jquery','laydate'], function(){
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;

        //第一个实例,渲染数据
        table.render({
            elem: '#demo'
            ,height: 500
            ,url: '${pageContext.request.contextPath}/role/findAllRole.do'//数据接口
            ,method: 'POST'
            ,page: true//打开分页
            ,limit:10
            ,limits:[5,10,15,20,25,30,50]
            ,autoSort: true//禁用前端的自动排序，开启后端的服务器排序
            ,toolbar: '#toolbarDemo'
            ,cols: [[
                //表头
                {type:'checkbox',fixed: 'left'}
                ,{field: 'roleId', title: '编号', width:120, sort: true, fixed: 'left'}
                ,{field: 'roleName', title: '角色名称', width:100}
                ,{field: 'roleKey', title: '关键字', width:100}
                ,{field: 'roleSort', title: '显示顺序', width:120, sort: true}
                ,{field: 'status', title: '状态', width:100,
                    templet: function(d){
                        //得到当前行数据，并拼接成自定义模板
                        if(d.status=='0'){
                            //return '可用'
                            return '<a class="layui-btn  layui-btn-xs">可用</a>'
                        }if (d.status=='1')
                        {
                            return '<a class="layui-btn layui-btn-disabled  layui-btn-xs">不可用</a>'
                        }
                    }
                }
                ,{field: 'createBy', title: '创建人', width:100}
                ,{field: 'createTime', title: '创建时间', width:150}
                ,{field: 'updateBy', title: '修改人', width:100}
                ,{field: 'updateTime', title: '修改时间', width:150}
                ,{fixed: 'right', title: '操作', width: 150, align:'center', toolbar: '#barDemo'}
            ]]
        });

        //触发排序事件
        table.on('sort(test)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值 "
            console.log(obj.field); //当前排序的字段名
            console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
            console.log(this); //当前排序的 th 对象

            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
            table.reload('demo', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,type: 'post'
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                }
                ,method: 'post'
            });
        });

        //模糊查询,监听提交
        form.on('submit(formDemo)', function(data){
            table.reload('demo',{
                where: {
                    roleName: data.field.roleName
                    ,createBy: data.field.createBy
                    ,updateBy: data.field.updateBy
                }
                ,page: {
                    curr: 1
                }
            });
            return false;
        });

        //重置按钮绑定单击事件
        $("#search_reset").click(function(){
            table.reload('demo',{
                where: {
                    roleName: ""
                    ,createBy: ""
                    ,updateBy: ""
                },
                page: {
                    curr: 1
                }
            });
        });


        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'add':
                    layer.open({
                        title: '添加角色',
                        type: 1,
                        content: $('#addRoleFrom'),
                        area: ['500px', '500px']
                    });
                    form.val('addRoleFromFilter',{
                        "roleName": ""
                        ,"roleKey": ""
                        ,"roleSort": ""
                    });

                    //监听addUserForm表单的提交事件toolbarDemo
                    form.on('submit(addRoleFrom)', function (data) {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/role/addRole.do"
                            , data: data.field
                            , dataType: "json"
                            , type: "post"
                            , success: function (redata) {
                                if (redata.code == 0) {;
                                    layer.closeAll();//关闭对话框
                                    table.reload('demo', {
                                        page: {
                                            curr: 1
                                        }
                                    });
                                }
                                layer.msg(redata.msg);
                            }
                        });
                        return false;
                    });
                    break;
                case  'delete':
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有选中的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i=0;i<data.length;i++) {
                            ids[i] = data[i].roleId;
                        }
                        console.log(data)
                        layer.confirm('真的删除选中行吗？',function (index){
                            $.ajax({
                                url: '${pageContext.request.contextPath}/role/delete.do'
                                ,data: {roleId:ids.join(",")}
                                ,dataType: "JSON"
                                ,type: "POST"
                                ,success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();
                                        //刷新表格数据
                                        table.reload('demo',{
                                            page:{
                                                curr:1 //重新从第一页开始
                                            }
                                        });
                                    }
                                    layer.msg(data.msg)
                                }
                            });
                        });
                    }
                    break;
            }
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值
            // 回显状态开关
            if (data.status.valueOf() == "1"){
                $("#updateRoleForm input[type=checkbox]").prop("checked", "");
            } else {
                $("#updateRoleForm input[type=checkbox]").prop("checked", "checked");
            }
            if (layEvent === 'update') {
                layer.open({
                    title: '修改角色信息'
                    ,type: 1
                    ,content: $('#updateRoleForm')
                    ,area: ["500px","500px"]
                });

                //回显用户信息
                form.val('updateRoleFilter',{
                    "roleId": data.roleId
                    ,"roleName": data.roleName
                    ,"roleKey": data.roleKey
                    ,"roleSort": data.roleSort
                });


                //日期
                laydate.render({
                    elem: '#date1'
                });
                laydate.render({
                    elem: '#date2'
                });
                // 更新用户信息
                form.on('submit(updateRole)',function (data) {
                    $.ajax({
                        url: "${pageContext.request.contextPath}/role/updateRole.do"
                        ,data: data.field
                        ,dataType: "json"
                        ,type: "post"
                        ,success: function (redata) {
                            if (redata.code == 0) {
                                layer.closeAll();//关闭对话框
                                layer.msg(redata.msg);
                                table.reload('demo',{
                                    page: {
                                        curr: 1
                                    }
                                });
                                // 改变状态开关
                                if (data.status.valueOf() == "1"){
                                    $("#updateRoleForm input[type=checkbox]").prop("checked", "");
                                } else {
                                    $("#updateRoleForm input[type=checkbox]").prop("checked", "checked");
                                }

                            } else {
                                layer.msg(redata.msg);
                            }
                        }
                    });
                    return false;
                });
            } else if (layEvent === 'del') {
                layer.confirm('真的删除吗？',function (index){
                    console.log(index)
                    $.ajax({
                        url: "${pageContext.request.contextPath}/role/delete.do"
                        ,dataType: "JSON"
                        ,type: "POST"
                        ,data: {roleId: data.roleId}
                        ,success: function (data) {
                            if (data.code==0) {
                                layer.closeAll(); //疯狂模式，关闭所有层
                                table.reload('demo',{
                                    page:{
                                        curr:1 //重新从第一页开始
                                    }
                                });
                            }
                            layer.msg(data.msg);
                        }
                    });
                })
            }


        });
    });
</script>
</html>

